package com.gf.gf_data.controller;

import com.gf.gf_data.dto.AccessToken;
import com.gf.gf_data.dto.BaseResult;
import com.gf.gf_data.dto.ResultMessage;
import com.gf.gf_data.sys.annotation.Access;
import com.gf.gf_data.util.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * token 控制处理器
 *
 * Created by Eternal_L on 2018/1/10.
 */
@RestController
@RequestMapping("/user")
public class TokenController {

    private static Logger log = LoggerFactory.getLogger(TokenController.class);

    /**
     * 记住密码时验证token，更换token
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/check", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResult checkToken(HttpServletRequest request){
        log.info("invoke-------------/user/check.get");
        // 创建、返回新的accessToken
        AccessToken accessTokenEntity = new AccessToken();
        accessTokenEntity.setAccessToken((String) request.getAttribute("new_token"));
        accessTokenEntity.setTokenType(JWTUtil.TOKEN_TYPE);
        accessTokenEntity.setExpires(3600);
        return new BaseResult<>(true, accessTokenEntity);
    }


    @GetMapping(value = "/ex", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResult failure(){
        log.info("invoke-------------/user/ex.get");

        return new BaseResult<>(true, ResultMessage.TOKEN_DATA_ID_ABNORMAL.getState(),
                ResultMessage.TOKEN_DATA_ID_ABNORMAL.getMsg());
    }


    @GetMapping(value = "/getaccount", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResult testAA(){
        log.info("====================>>>>>>/user/getaccount.get");
        return new BaseResult<>(true,  "OK");
    }

    @Access({"admin"})
    @GetMapping(value = "/hh", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public BaseResult testccc(){
        log.info("invoke-------------/test/hh.get");
        return new BaseResult<>(true,  "Successful");
    }

}
